package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.entity.StudentInfo;

public class StudentInfoDao extends BaseDao{
	
	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	
	public List<StudentInfo> Sele(){
		List<StudentInfo> list=new ArrayList<StudentInfo>();
		
		try {
			conn=super.getConnection();
			String sql="select * from studentInfo order by sid desc";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while (rs.next()) {
				StudentInfo studentInfo=new StudentInfo();
				studentInfo.setSaddress(rs.getString("saddress"));
				studentInfo.setSage(rs.getInt("sage"));
				studentInfo.setSemail(rs.getString("semail"));
				studentInfo.setSgender(rs.getString("sgender"));
				studentInfo.setSid(rs.getInt("sid"));
				studentInfo.setSname(rs.getString("sname"));
				list.add(studentInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}
	
	public StudentInfo SeleId(int id){
		StudentInfo studentInfo=null;
		
		try {
			conn=super.getConnection();
			String sql="select * from studentInfo where sid=? order by sid desc";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, id);
			rs=pstmt.executeQuery();
			if (rs.next()) {
				studentInfo=new StudentInfo();
				studentInfo.setSaddress(rs.getString("saddress"));
				studentInfo.setSage(rs.getInt("sage"));
				studentInfo.setSemail(rs.getString("semail"));
				studentInfo.setSgender(rs.getString("sgender"));
				studentInfo.setSid(rs.getInt("sid"));
				studentInfo.setSname(rs.getString("sname"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return studentInfo;
	}
	
	public int Upda(StudentInfo info){
		int result=0;
		try {
			conn=super.getConnection();
			String sql="update studentInfo set sname=?,sgender=?,sage=?,saddress=?,semail=? where sid=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1,info.getSname() );
			pstmt.setObject(2,info.getSgender() );
			pstmt.setObject(3,info.getSage() );
			pstmt.setObject(4,info.getSaddress() );
			pstmt.setObject(5,info.getSemail() );
			pstmt.setObject(6,info.getSid() );
			result=pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}
	
	public int Dele(int id){
		int result=0;
		try {
			conn=super.getConnection();
			String sql="delete from studentInfo where sid=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1,id);
			result=pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}

}
